#!/bin/sh

run_char=1
run_check=1
current_output=output

trap '{ cleanup $current_output $new_output; exit 1; }' INT

##==============================================================================
##
## Get command line options that start with dash:
##
##==============================================================================
for opt
do

  arg=`expr "x$opt" : 'x[^=]*=\(.*\)'`

  case $opt in

    -h | --help)
      help=1
      ;;

    --runs=*)
        run_char=0
        run_size=0
        run_check=0
        runs=`echo $arg | sed 's/:/ /g'`
        for i in $runs
        do
            case $i in
                char)
                    run_char=1
                    ;;
                check)
                    run_check=1
                    ;;
                all)
                    run_char=1
                    run_check=1
                    ;;
                *)
                    echo "$0: unknown option argument for --runs:  $i"
                    exit 1
            esac
        done
        ;;
      
    *)
      echo "$0: unknown option:  $opt"
      exit 1
      ;;

  esac
done

##=============================================================================
##
## Handle --help option:
##
##==============================================================================

if [ "$help" = "1" ]; then

    cat<<EOF

Usage: ./regress [OPTIONS] TESTNAME(S)

OVERVIEW:

Runs regression tests for OMI.

    $ ./regress [OPTIONS] TESTNAME(S)

OPTIONS:
    -h, --help              Print this help message.
    --enable-ut             Enable UT tests.
    --runs=TESTNAME(S)  Run only those test runs
TESTNAME(S):
    char                  run (single byte) char tests run
    check                 run check tests run
    all                   run all test runs (char, check)

Examples:
       ./regress
       ./regress --runs=char
       
EOF
    exit 0
fi

##==============================================================================
##
## Determine configure and root directories:
##
##==============================================================================

root=`dirname $0`

##==============================================================================
##
## Cleanup function
##
##==============================================================================
cleanup() 
{ 
    ./tools/auth_tests_cleanup.sh $1 $2
}

##==============================================================================
##
## Create soft-link to $OMI_USER account
##
##==============================================================================
createlink()
{ 
    username=$OMI_USER

# First, check if $OMI_USER's home directory is writable
    if [ "x$username" != "x" ]; then
        userhome=`eval echo ~$username`
        if [ -w $userhome ]; then
            new_output="$userhome/$USER-$current_output"
            sudo rm -rf $new_output
            mkdir $new_output
            if [ $? -eq 0 ]; then
                ln -s $new_output $current_output
            fi
        fi
    fi

# If that's not available, try /tmp
    new_output="/tmp/$USER-$current_output"
    sudo rm -rf $new_output
    mkdir $new_output
    if [ $? -eq 0 ]; then
        ln -s $new_output $current_output
    else
        echo "Unable to create softlink to $current_output.  Skipping..."
    fi
}

##==============================================================================
##
## 'world' tests
##
##==============================================================================

if [ "$run_char" = "1" ]; then

current_output=output1
sudo rm -rf ./$current_output GNUmakefile
createlink
$root/configure --dev --outputdirname=$current_output $options
make world

if [ "$?" != "0" ]; then
    echo "$0: failed 'world' tests"
    cleanup $current_output
    exit 1
fi

sudo rm -rf ./$current_output $new_output GNUmakefile

fi

##==============================================================================
##
## make dist + check tests
##
##==============================================================================

if [ "$run_check" = "1" ]; then

current_output=output4
sudo rm -rf ./$current_output GNUmakefile

# compose omi tar package name
version="`$root/configure --show-version`"
omitarname=omi-$version.tar.gz

echo "OMI Tar Name is $omitarname"

# create tar package
$root/configure
make dist

# copy tar package to a temp directory
checkdir=$root/disttemp
sudo rm -rf $checkdir
mkdir $checkdir
cp $root/$omitarname $checkdir

# validate the tar package
cd $checkdir
ls $omitarname
gzip -d $omitarname
tar xf omi-$version.tar
cd omi-$version

##
## Create the prefix directory:
##

prefix=/tmp/omi2-$USER
sudo rm -rf $prefix

if [ -d "$prefix" ]; then
    echo "$0: failed: directory already exists (please remove and retry): $prefix"
    cleanup ./$current_output
    exit 1;
fi

##
## configure:
##

sudo rm -rf $prefix
./configure --enable-werror --prefix=$prefix --outputdirname=$current_output $options

if [ "$?" != "0" ]; then
    echo "$0: configure failed"
    cleanup ./$current_output
    exit 1
fi

##
## make:
##

make

if [ "$?" != "0" ]; then
    echo "$0: make failed"
    cleanup ./$current_output
    exit 1
fi

##
## check:
##

make check

if [ "$?" != "0" ]; then
    echo "$0: check failed"
    cleanup ./$current_output
    exit 1
fi

##
## install:
##

make install

if [ "$?" != "0" ]; then
    echo "$0: make install failed"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Generate C++ provider:
##

mkdir $prefix/ocsprovider

if [ "$?" != "0" ]; then
    echo "$0: failed to create $prefix/oscprovider directory"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

cat > $prefix/ocsprovider/schema.mof  <<EOF
class OMI_ComputerSystem : CIM_ComputerSystem { };
EOF

if [ "$?" != "0" ]; then
    echo "$0: failed to create schema.mof"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

savedir=`pwd`

cd $prefix/ocsprovider
$prefix/bin/omigen --cpp -m oscprovider ./schema.mof OMI_ComputerSystem

if [ "$?" != "0" ]; then
    echo "$0: failed to generate provider"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Build the C++ provider:
##

make

if [ "$?" != "0" ]; then
    echo "$0: failed to build provider"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Register the C++ provider:
##

make reg

if [ "$?" != "0" ]; then
    echo "$0: failed to register provider"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Start the server with these options:
##     -i -- ignore authentication (allowing to run as non-root)
##     -d -- daemonize (run in the background)
##     --livetime 60 -- exit server in 60 seconds
##     --httpport 0 -- don't bind to an HTTP port
##     --httpports 0 -- don't bind to an HTTPS port
##

$prefix/bin/omiserver -i -d --livetime 60 --httpport 0 --httpsport 0

if [ "$?" != "0" ]; then
    echo "$0: failed to start server"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

sleep 3

##
## Enumerate instances of OMI_ComputerSystem (expect MI_RESULT_NOT_SUPPORTED=7)
##

$prefix/bin/omicli ei root/cimv2 OMI_ComputerSystem

if [ "$?" != "7" ]; then
    echo "$0: failed to enumerate instances of OMI_ComputerSystem"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Stop the server:
##

$prefix/bin/omiserver -s

if [ "$?" != "0" ]; then
    echo "$0: failed to stop the server"
    sudo rm -rf $prefix
    cleanup ./$current_output
    exit 1
fi

##
## Cleanup:
##

cd $savedir
sudo rm -rf $prefix
sudo rm -rf ./$current_output GNUmakefile
cd ../..
sudo rm -rf $root/$omitarname $root/GNUmakefile $checkdir

fi

##==============================================================================
##
## SUCCESS
##
##==============================================================================

echo ""
echo ""
echo "##===================##"
echo "##                   ##"
echo "## ALL TESTS PASSED! ##"
echo "##                   ##"
echo "##===================##"
echo ""
exit 0
